import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UdpCilent {
   private DatagramSocket socket=null;
   private String ServiceIP;
   private int ServicePort;
   public UdpCilent(String serviceIP,int servicePort) throws SocketException {
       this.ServiceIP=serviceIP;
       this.ServicePort=servicePort;
       socket=new DatagramSocket();
   }
   public void start() throws IOException {
       System.out.println("客户端启动");
       Scanner scanner=new Scanner(System.in);
       while (true){
           System.out.println("->");
           String request= scanner.next();
           DatagramPacket requestPacket=new DatagramPacket(request.getBytes(),request.getBytes().length,
                   InetAddress.getByName(this.ServiceIP),
                   this.ServicePort);
           socket.send(requestPacket);
           DatagramPacket responsePacket=new DatagramPacket(new byte[4096],4096);
           socket.receive(responsePacket);
           String response=new String(responsePacket.getData(),0,responsePacket.getLength());
           System.out.println(response);
       }
   }

    public static void main(String[] args) throws IOException {
        UdpCilent cilent=new UdpCilent("127.0.0.1",9090);
        cilent.start();
    }
}
